package com.silverbox.sweeporder.common.util;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.silverbox.core.util.StringUtils;
import com.silverbox.shopper.entity.AlipayApplyAuthToken;
import com.silverbox.shopper.entity.ShopperExtend;

public class AlipayUtils {

	/**
	 * 初始话支付宝支付客户端
	 * @param shopperExtend
	 * @param authToken
	 * @param payMethod
	 * @return
	 */
	public static AlipayClient initAlipayClient(String PRIVATE_KEY,String ALIPAY_PUBLIC_KEY,ShopperExtend shopperExtend,AlipayApplyAuthToken authToken,int payMethod) {
		String appId = shopperExtend.getAlipayAppId();
		String privateKey = shopperExtend.getAlipayPrivateKey();
		String publicKey = shopperExtend.getAlipayAlipayPublicKey();
		if(payMethod==1 && (StringUtils.isEmpty(appId)||StringUtils.isEmpty(privateKey)||StringUtils.isEmpty(publicKey))){
			payMethod = 2;
		}
		if(payMethod==2 && authToken!=null) {
			appId=authToken.getAppId();
			privateKey = PRIVATE_KEY;
			publicKey = ALIPAY_PUBLIC_KEY;
		}
		return new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",appId, 
				privateKey, "json", "UTF-8",publicKey);
	}
	
	public static AlipayClient initAlipayClientByAppId(String PRIVATE_KEY,String ALIPAY_PUBLIC_KEY,String appId) {
		return new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",appId, 
				PRIVATE_KEY, "json", "UTF-8",ALIPAY_PUBLIC_KEY);
	}
	
}
